package com.hk.core.jdbc.tenants;

import com.hk.commons.util.StringUtils;

/**
 * @author Kevin
 * @date 2024-04-11 22:32
 */
public final class DefaultTenantInfoAware implements UserTenantAware {

    private final String defaultTenantId;

    private final ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public DefaultTenantInfoAware(String defaultTenantId) {
        this.defaultTenantId = defaultTenantId;
    }

    @Override
    public void setCurrentTenantId(String tenantId) {
        threadLocal.set(tenantId);
    }

    @Override
    public String getCurrentTenantId() {
        String value = threadLocal.get();
        return StringUtils.hasLength(value) ? value : defaultTenantId;
    }

    @Override
    public void clearCurrentTenantId() {
        threadLocal.remove();
    }
}
